package com.model.response;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
@Accessors(chain = true)
public class Response<V> {
    private int code;
    private String msg;
    private V data;

    public static final int SUCCESS_CODE = 200;
    public static final int FAIL_CODE = 50000;
    public static final int DENIED_CODE = 666;

    public static final String SUCCESS_MSG = "ok";
    public static final String FAIL_MSG = "fail";
    public static final String DENIED_MSG = "denied";

    public Response putKV(String key, Object val) {
        if (!(data instanceof Map)) {
            data = (V) new HashMap<String, Object>();
        }
        ((Map) data).put(key, val);
        return this;
    }

    public static Response success() {
        return new Response().setCode(SUCCESS_CODE).setMsg(SUCCESS_MSG);
    }

    public static Response fail() {
        return new Response().setCode(FAIL_CODE).setMsg(FAIL_MSG);
    }

    public static Response Denied() {
        return new Response().setCode(DENIED_CODE).setMsg(DENIED_MSG);
    }

    public static Response fail(String msg) {
        return new Response().setCode(FAIL_CODE).setMsg(msg);
    }

    public static Response Denied(String msg) {
        return new Response().setCode(DENIED_CODE).setMsg(msg);
    }

    public static <V>Response<V> success(V data) {
        Response<V> response = Response.success();
        return response.setData(data);
    }

    public static <V>Response<V> success(V data, String msg) {
        return new Response().setCode(SUCCESS_CODE).setMsg(msg).setData(data);
    }


}
